﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Ajax_HocVienAjax : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["Methods"] == null)
        {
            Response.Write("{\"Status\":\"Error\"}");
            Response.End();
        }

        switch (Request["Methods"])
        {
            case "DanhSach":
                DanhSach();
                break;
            case "ThemHocVienLop":
                ThemHocVienLop();
                break;
            case "XoaHocVienLop":
                XoaHocVienLop();
                break;
            case "XemHocVienLop":
                XemHocVienLop();
                break;
            case "CapNhatHocVienLop":
                CapNhatHocVienLop();
                break;
            default:
                break;
        }
    }

    public void DanhSach()
    {
        string sCauHoi = Request["CauHoi"];
        string sDapAn = Request["DapAn"];

        JavaScriptSerializer serial = new JavaScriptSerializer();
        List<HocVienINFO> dsHocVien = HocVienDAL.listAll();
        Response.Write(serial.Serialize(dsHocVien));
        Response.End();
    }

    public void ThemHocVienLop()
    {
        int hocvien = int.Parse(Request.Params["HocVien"]);
        int lop = int.Parse(Request.Params["Lop"]);

        if (hocvien <= 0 || lop <= 0) {
            Response.Write("{\"Status\":\"Error\",\"Mess\":\"Không thể thêm dữ liệu!\"}");
            Response.End();
        }
        HocVien_LopINFO curr = HocVien_LopDAL.GetInfo(hocvien,lop);
        if(curr != null && curr.MaLop != 0){
            Response.Write("{\"Status\":\"Error\",\"Mess\":\"Học viên đã được thêm!\"}");
            Response.End();
        }
        HocVien_LopINFO obj = new HocVien_LopINFO();
        obj.MaHV = hocvien;
        obj.MaLop = lop;
        obj.SoBuoiNghi = 0;
        obj.TinhTrang = 1;
        if (HocVien_LopDAL.Insert(obj) > 0)
        {
            Response.Write("{\"Status\":\"Success\"}");
        }
        else {
            Response.Write("{\"Status\":\"Error\",\"Mess\":\"Không thể thêm dữ liệu!\"}");
        }
        
        Response.End();
    }

    public void XoaHocVienLop()
    {
        int hocvien = int.Parse(Request.Params["HocVien"]);
        int lop = int.Parse(Request.Params["Lop"]);

        if (hocvien <= 0 || lop <= 0)
        {
            Response.Write("{\"Status\":\"Error\",\"Mess\":\"Sai thông tin!\"}");
            Response.End();
        }
        if (HocVien_LopDAL.Delete(hocvien,lop) > 0)
        {
            Response.Write("{\"Status\":\"Success\"}");
        }
        else
        {
            Response.Write("{\"Status\":\"Error\",\"Mess\":\"Không thể xóa dữ liệu!\"}");
        }

        Response.End();
    }

    public void XemHocVienLop()
    {
        int hocvien = int.Parse(Request.Params["HocVien"]);
        int lop = int.Parse(Request.Params["Lop"]);

        JavaScriptSerializer serial = new JavaScriptSerializer();
        HocVien_LopINFO HocVien = HocVien_LopDAL.GetInfo(hocvien,lop);
        Response.Write(serial.Serialize(HocVien));
        Response.End();
    }

    public void CapNhatHocVienLop()
    {
        if(Request.Params["HocVien"] ==  null || Request.Params["Lop"] == null || Request.Params["SoBuoiNghi"] == null || Request.Params["TinhTrang"] == null)
        {
            Response.Write("{\"Status\":\"Error\",\"Mess\":\"Thiếu thông tin!\"}");
            Response.End();
        }
        int hocvien = int.Parse(Request.Params["HocVien"]);
        int lop = int.Parse(Request.Params["Lop"]);
        int BuoiNghi = int.Parse(Request.Params["SoBuoiNghi"]);
        int TinhTrang = int.Parse(Request.Params["TinhTrang"]);

        if (hocvien <= 0 || lop <= 0)
        {
            Response.Write("{\"Status\":\"Error\",\"Mess\":\"Không thể thêm dữ liệu!\"}");
            Response.End();
        }
        HocVien_LopINFO obj = HocVien_LopDAL.GetInfo(hocvien,lop);
        obj.SoBuoiNghi = BuoiNghi;
        obj.TinhTrang = TinhTrang;
        if (HocVien_LopDAL.Update(obj) > 0)
        {
            Response.Write("{\"Status\":\"Success\"}");
        }
        else
        {
            Response.Write("{\"Status\":\"Error\",\"Mess\":\"Không thể cập nhật!\"}");
        }

        Response.End();
    }
}